package com.longer.demo.biz.service.impl;

import com.longer.demo.biz.domain.UserDO;
import com.longer.demo.biz.domain.UserPassword;
import com.longer.demo.biz.repository.UserDOMapper;
import com.longer.demo.biz.repository.UserPasswordMapper;
import com.longer.demo.biz.service.UserService;
import com.longer.demo.biz.service.model.UserModel;
import com.longer.demo.framework.error.BusinessError;
import com.longer.demo.framework.error.BusinessException;
import com.longer.demo.framework.validator.MyValidator;
import com.longer.demo.framework.validator.ValidationResult;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * @program: demo
 * @description:
 * @author: SunBiaoLong
 * @create: 2019-01-11 11:01
 **/
@Service
public class UserServiceImpl implements UserService {

	@Autowired(required = false)
	private UserDOMapper userDOMapper;

	@Autowired
	private MyValidator validator;
	@Autowired(required = false)
	private UserPasswordMapper userPasswordMapper;

	@Override
	public UserModel validate(String phone, String encryptPassword) throws BusinessException {
		if (StringUtils.isEmpty(phone)
				||StringUtils.isEmpty(encryptPassword)){
			throw new BusinessException(BusinessError.PARAMETER_VALIDATION_ERROR);
		}
		UserDO userDO = userDOMapper.selectByPhone(phone);
		if (userDO==null){
			throw new BusinessException(BusinessError.LOGIN_ERROR);
		}
		UserPassword userPassword = userPasswordMapper.selectByUserId(userDO.getId());
		if (!com.alibaba.druid.util.StringUtils.equals(userPassword.getEncryptPassword(),encryptPassword)){
			throw new BusinessException(BusinessError.LOGIN_ERROR);
		}
		return convertFromDataDomain(userDO, userPassword);

	}

	/**
	 * 避免异常情况下插入了用户表,但是没有插入密码的情况
	 *
	 * @param userModel
	 * @throws BusinessException
	 */
	@Transactional
	@Override
	public void register(UserModel userModel) throws BusinessException {
		if (userModel == null) {
			throw new BusinessException(BusinessError.PARAMETER_VALIDATION_ERROR);
		}
//原始的校验方式
//		if (StringUtils.isEmpty(userModel.getName())
//				|| StringUtils.isEmpty(userModel.getPhone())
//				|| userModel.getGender() == null
//				|| userModel.getAge() == null) {
//			throw new BusinessException(BusinessError.PARAMETER_VALIDATION_ERROR);
//		}

		ValidationResult validationResult=validator.validate(userModel);
		if (validationResult.isHasErrors()){
			throw new BusinessException(BusinessError.PARAMETER_VALIDATION_ERROR, validationResult.getErrorMsg());
		}

		//model-->domain
		UserDO userDO = convertFromUserModel(userModel);
		// 在xml中要设置   keyProperty="id" useGeneratedKeys="true"
		try {
			userDOMapper.insertSelective(userDO);
		} catch (DuplicateKeyException ex) {
			throw new BusinessException(BusinessError.PARAMETER_VALIDATION_ERROR, "该手机号已注册");
		}

		//获取从数据库中获取的id
		userModel.setId(userDO.getId());
		UserPassword userPassword = convertFromUserModelToPassword(userModel);
		userPasswordMapper.insertSelective(userPassword);
	}

	private UserPassword convertFromUserModelToPassword(UserModel userModel) {
		if (userModel == null) {
			return null;
		}
		UserPassword userPassword = new UserPassword();
		userPassword.setUserId(userModel.getId());
		userPassword.setEncryptPassword(userModel.getEncryptPassword());
		return userPassword;
	}

	private UserDO convertFromUserModel(UserModel userModel) {
		if (userModel == null) {
			return null;
		}
		UserDO userDO = new UserDO();
		BeanUtils.copyProperties(userModel, userDO);
		return userDO;
	}

	@Override
	public UserModel getUserModelById(Integer id) {
		UserDO userDO = userDOMapper.selectByPrimaryKey(id);
		if (userDO == null) {
			return null;
		}
		UserPassword userPassword = userPasswordMapper.selectByUserId(userDO.getId());
		return convertFromDataDomain(userDO, userPassword);
	}

	/**
	 * 将userinfo和userpassword组合成suermodel
	 *
	 * @param userDO
	 * @param userPassword
	 * @return
	 */
	private UserModel convertFromDataDomain(UserDO userDO, UserPassword userPassword) {
		if (userDO == null) {
			return null;
		}

		UserModel userModel = new UserModel();
		BeanUtils.copyProperties(userDO, userModel);
		if (userPassword != null) {
			userModel.setEncryptPassword(userPassword.getEncryptPassword());
		}

		return userModel;

	}
}
